﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Text;
using System.Threading.Tasks;

namespace EONFULLSIN2.Services.DB
{
    /// <summary>
    /// DB操作接口
    /// </summary>
    /// <typeparam name="DbContext"></typeparam>
    public interface IDbOperate<DbContext> where DbContext: IFreeSqlContext
    {
        /// <summary>
        /// 获取数据块连接上下文
        /// </summary>
        /// <returns></returns>
        IFreeSql GetFreeSql();

        /// <summary>
        /// 获取单个对象
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="exp"></param>
        /// <returns></returns>
        T GetModel<T>(Expression<Func<T, bool>> exp) where T : class, new();

        /// <summary>
        /// 获取对象列表
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="exp"></param>
        /// <returns></returns>
        List<T> GetModelList<T>(Expression<Func<T, bool>> exp) where T : class, new();

        /// <summary>
        /// 新增对象
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="model"></param>
        /// <returns></returns>
        bool AddModel<T>(T model) where T : class, new();

        /// <summary>
        /// 新增对象列表
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="model"></param>
        /// <returns></returns>
        bool AddModels<T>(List<T> models) where T : class, new();

        /// <summary>
        /// 删除对象
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="exp"></param>
        /// <returns></returns>
        bool DeleteModel<T>(Expression<Func<T, bool>> exp) where T : class, new();

        /// <summary>
        /// 更新指定字段，调用示例 UpdateModel&lt;User&gt;(o => o.Account == 'xxx', o => new User { PassWord = 'xxxx' });
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="exp"></param>
        /// <param name="exp2"></param>
        /// <returns></returns>
        bool UpdateModel<T>(Expression<Func<T, bool>> exp, Expression<Func<T, T>> exp2) where T : class, new();

        /// <summary>
        /// 更新整个对象，会自动查找主键 调用示例 UpdateModelSuper&lt;User&gt;(o => true, model);
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="exp"></param>
        /// <param name="t"></param>
        /// <returns></returns>
        bool UpdateModelSuper<T>(Expression<Func<T, bool>> exp, T t) where T : class, new();

        /// <summary>
        /// 判断是否存在
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="exp"></param>
        /// <returns></returns>
        bool CheckExist<T>(Expression<Func<T, bool>> exp) where T : class, new();

        /// <summary>
        /// 执行SQL语句
        /// </summary>
        /// <param name="sql"></param>
        /// <returns></returns>
        bool ExceSql(string sql);

        /// <summary>
        /// 执行SQL语句返回对象列表
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="sql"></param>
        /// <returns></returns>
        List<T> ExceSqlToList<T>(string sql) where T : class, new();
    }



}
